﻿using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ViewLearn
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<ProductInfo> _products = new List<ProductInfo>()
        {
            new ProductInfo()
            {
                ProductId = 1,
                ProductName = "每日坚果",
                Price = 44.90m,
                ManuDate = new DateTime(2025,1,13),
                Des = "500g混合坚果干果果仁礼盒小包",
                ImagePath = "每日坚果.webp"
            },
            new ProductInfo()
            {
                ProductId = 2,
                ProductName = "不多言",
                Price = 19.9m,
                ManuDate = new DateTime(2025,6,25),
                Des = "坚果芙饼干巴旦木椰片芙脆曲奇单独小包装干酪蛋糕网红休闲小零食",
                ImagePath = "不多言.jpg"
            },
            new ProductInfo()
            {
                ProductId = 2,
                ProductName = "杨先生",
                Price = 12,
                ManuDate = new DateTime(2025,4,5),
                Des = "芡实糕点营养早餐",
                ImagePath = "杨先生.webp"
            }
        };

        ListCollectionView _view = null;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = _products;
            _view = (ListCollectionView)CollectionViewSource.GetDefaultView(this.DataContext);//检索视图对象
            this.combo.ItemsSource = _products;//在这里给combo加绑定
            view_CurrentChanged(null, null);
        }

        /// <summary>
        /// 当视图的当前项（CurrentItem）发生变化时触发
        /// 当ICollctionView中的CurrentItem、CurrentPosition发生了改变，就会触发CurrentChanged事件。
        /// 
        /// 如果它是ComboBox的数据源，重新选择ComboBox的选项，这个时间是不会发生变化的，需要自己手动触发，
        /// 或者自己把ItemsControl.IsSynchronizedWithCurrentItem属性设置为true。设置之后，当前选择的项
        /// 会被自动同步，从而匹配视图的当前位置，且不需要使用任何额外的代码。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void view_CurrentChanged(object? sender, EventArgs e)
        {
            lblPosition.Text = $"第{_view.CurrentPosition + 1}/{_view.Count}页";
            cmbPrev.IsEnabled = _view.CurrentPosition > 0;
            cmbNext.IsEnabled = _view.CurrentPosition < _view.Count - 1;
        }

        private void cmbPrev_Click(object sender, RoutedEventArgs e)
        {
            _view.MoveCurrentToPrevious();
        }

        private void cmbNext_Click(object sender, RoutedEventArgs e)
        {
            _view.MoveCurrentToNext();
        }
    }
}